package com.aaa.controller;

import com.aaa.entity.CourseTypeEntity;
import com.aaa.entity.Message;
import com.aaa.entity.Pager;
import com.aaa.service.CourseTypeService;
import com.aaa.service.CourseTypeServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "CourseTypeServlet",urlPatterns ="/courseType.action" )
public class CourseTypeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null) {
            switch (method) {
                case "queryAll":
                    queryAll(request, response);
                    break;
                case "add":
                    add(request,response);
                    break;
                case "queryOne":
                    queryOne(request,response);
                    break;
                case "delete":
                    delete(request,response);
                    break;
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }

    protected void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sPageNo=request.getParameter("pageNo");
        if (sPageNo==null) sPageNo="1";
        int pageNo= Integer.parseInt(sPageNo);
        CourseTypeService service=new CourseTypeServiceImpl();
        Pager pager=service.queryAll(pageNo);
       String str= JSON.toJSONStringWithDateFormat(pager,"yyyy-MM-dd");
       response.getWriter().print(str);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String type_id=request.getParameter("id");
        int Id=type_id==""?0: Integer.parseInt(type_id);
        String type_name=request.getParameter("type_name");
        CourseTypeEntity courseType=new CourseTypeEntity();
        courseType.setTypeId(Id);
        courseType.setTypeName(type_name);
        Message message=new Message();

        CourseTypeService service=new CourseTypeServiceImpl();
        message=service.Update(courseType);
        String str=JSON.toJSONString(message);
        response.getWriter().print(str);
    }

    protected void queryOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
       String id=request.getParameter("id");
       int type_id= Integer.parseInt(id);
       CourseTypeService service=new CourseTypeServiceImpl();
       Map map=service.queryOne(type_id);
       String str=JSON.toJSONString(map);
       response.getWriter().print(str);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        int type_id= Integer.parseInt(id);
        CourseTypeService service=new CourseTypeServiceImpl();
       int result=service.delete(type_id);
       response.getWriter().print(result);

    }

}
